<?php

namespace App\Http\Controllers\admin;

use Validator;
use App\Model\admin\CollegeDefault;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Mockery\Exception;

class CollegeDefaultController extends Controller
{

    /**
     * 新增学院
     * @param Request $request
     * @return mixed
     */
    public function createCollege(Request $request)
    {
        $result = $this->result();
        $request = $this->getRequest($request);

        try
        {
            $Validator = Validator::make($request, [
               'college' => 'required | unique:college_defaults',
            ]);

            if($Validator->fails())
            {
                $errors = $Validator->errors();
                throw new Exception($errors->first('college'), config('result.errorCreateCollegeDefaultCode'));
            }

            $params['college'] = trim($request['college']);

            $College = new CollegeDefault();
            $College->create($params);
        }
        catch (Exception $exception)
        {
            $result['errorCode'] = $exception->getCode();
            $result['errorMsg']  = $exception->getMessage();
        }

        return $result;
    }

    /**
     * 获取学院列表
     * @return mixed
     * errorCode,errorMsg,data,total,current_page,last_page,next_page_url,prev_page_url
     */
    public function getCollegeDefaultList()
    {
        $result = $this->result();
        $data = array();
        try
        {
            $grades = new CollegeDefault();
            $data = $grades->select(['id', 'college'])->paginate(config('app.pageSize'))->toArray();
            if(empty($data['data']))
            {
                throw new Exception(config('result.errorGetCollegeDefaultMsg'), config('result.errorGetCollegeDefaultCode'));
            }
            //获取的数据
            $result['data']          = $data['data'];
            //总条数
            $result['total']         = $data['total'];
            //当前页码
            $result['current_page']  = $data['current_page'];
            //最后一页页码
            $result['last_page']     = $data['last_page'];
            //下一页url
            $result['next_page_url'] = $data['next_page_url'];
            //上一页url
            $result['prev_page_url'] = $data['prev_page_url'];
        }
        catch (Exception $exception)
        {
            $result['errorCode'] = $exception->getCode();
            $result['errorMsg']  = $exception->getMessage();
        }

        return $result;
    }

    /**
     * 删除默认学院
     * @param CollegeDefault $collegeDefault
     * @return mixed
     */
    public function deleteCollegeDefault(CollegeDefault $collegeDefault)
    {
        $result = $this->result();

        try
        {
            $delete = $collegeDefault->where('id', $collegeDefault->id)->delete();
            if(!$delete)
            {
                throw new Exception(config('result.errorDeleteCollegeDefaultMsg'), config('result.errorDeleteCollegeDefaultCode'));
            }
        }
        catch (Exception $exception)
        {
            $result['errorCode'] = $exception->getCode();
            $result['errorMsg']  = $exception->getMessage();
        }

        return $result;
    }

    /**
     * 修改默认学院名称
     * @param Request $request
     * @param CollegeDefault $collegeDefault
     * @return mixed
     */
    public function modifyCollegeDefault(Request $request, CollegeDefault $collegeDefault)
    {
        $result = $this->result();
        $request = $this->getRequest($request);
        try
        {
            $Validator = Validator::make($request, [
                'college' => 'required | unique:college_defaults',
            ]);

            if($Validator->fails())
            {
                $errors = $Validator->errors();
                throw new Exception($errors->first('college'), config('result.errorModifyCollegeDefaultCode'));
            }

            $collegeDefault->college = $request['college'];
            $collegeDefault->save();
        }
        catch (Exception $exception)
        {
            $result['errorCode'] = $exception->getCode();
            $result['errorMsg']  = $exception->getMessage();
        }

        return $result;
    }
}
